
package article

import (
	"strings"
	"strconv"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Rt"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Core"
)


// validate input article data
func validateArticleData(data map[string]string, id string) string {
	if Rt.Validator.IsShorter(data["title"], 1) {
		return "标题至少一个字"
	}
	if !Rt.Validator.IsASCII(data["slug"]) {
		return "缩略名支持英文、数字和-"
	}
	if Rt.Validator.IsShorter(data["slug"], 2) {
		return "缩略名至少2个字"
	}
	if checkSlugExist(data["slug"], id) {
		return "缩略名重复"
	}
	if data["desc"] == "" {
		if !Rt.Validator.IsContain(data["content"], "[break]") {
			return "内容需要添加分隔符[break]，用于生成描述"
		}
	}
	return ""
}

// check article slug repeated
func checkSlugExist(slug string, id string) bool {
	sql := Core.Sql("fuxiaohei_articles", "id").Where("slug = ?").Select()
	data := Core.Db.One(sql, slug)
	if len(data) < 1 {
		return false
	}
	if id != "" {
		if data["id"] == id {
			return false
		}
	}
	return true
}

// add new article
func AddArticle(data map[string]string) int {
	// parse desc
	if data["desc"] == "" {
		data["desc"] = strings.Split(data["content"], "[break]")[0]
	}
	sql := Core.Sql("fuxiaohei_articles", "title,slug,desc,content,category,author,status,add_time").Insert()
	id := Core.Db.Exec(sql, data["title"], data["slug"], data["desc"], data["content"], data["category"], data["author"], data["status"], Rt.Time.Now())
	go categoryCountUpdate()
	return id
}

// update one article by id
func UpdateArticle(id string, data map[string]string) {
	if data["desc"] == "" {
		data["desc"] = strings.Split(data["content"], "[break]")[0]
	}
	sql := Core.Sql("fuxiaohei_articles", "title,slug,desc,content,category,status").Where("id = ?").Update()
	Core.Db.Exec(sql, data["title"], data["slug"], data["desc"], data["content"], data["category"], data["status"], id)
	go categoryCountUpdate()
}

// get one article by id
func GetOneById(id string) map[string]string {
	sql := Core.Sql("fuxiaohei_articles").Where("id = ?").Select()
	return Core.Db.One(sql, id)
}

// get article list
// return list data and count all number
func adminList(page int, size int, condition map[string]interface {}, pageUrl string) ([]map[string]string, string) {
	sql := Core.Sql("fuxiaohei_articles", "id,title,slug,add_time,comments,views,status").Paged(page, size).Order("id DESC")
	sql2 := Core.Sql("fuxiaohei_articles")
	args := []interface {}{}
	if len(condition) > 0 {
		for k, v := range condition {
			sql.Where(k + " = ?")
			sql2.Where(k + " = ?")
			args = append(args, v)
		}
	}
	articles := Core.Db.All(sql.Select(), args...)
	counts := Core.Db.One(sql2.Count(), args...)
	if len(counts) < 1 {
		return articles, ""
	}
	count, _ := strconv.Atoi(counts["countNum"])
	return articles, Rt.Pager.Html(Rt.Pager.Calculate(page, size, count), pageUrl, 1)
}

// delete one article by id
func DeleteArticle(id string) {
	sql := Core.Sql("fuxiaohei_articles").Where("id = ?").Delete()
	Core.Db.Exec(sql, id)
	go categoryCountUpdate()
}

// get article status text
func StatusText(status string) string {
	if status == "published" {
		return "已发布"
	}
	if status == "archived" {
		return "归档"
	}
	if status == "draft" {
		return "草稿"
	}
	return "未知"
}


